package com.z.platform;

import com.z.platform.controller.Controller;
import com.z.platform.controller.ControllerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 21051
 */
public class DispatchServlet extends HttpServlet {

    /**
     * Default constructor.
     */
    public DispatchServlet() {
        // TODO Auto-generated constructor stub
        super();
    }

    /**
     * spring:
     * datasource:
     * #MySQL配置
     * driverClassName: com.mysql.cj.jdbc.Driver
     * url: jdbc:mysql://49.235.88.75:3306/gulimall_ums?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
     * username: zhanghaonan
     * password: hadoop123456
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String uri = req.getRequestURI();
        String contextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        uri = uri.substring((contextPath + servletPath).length());
        //从静态工厂根据对应的请求拿到对应的controller对象
        Controller controller = ControllerFactory.getController(uri);
        if (null != controller) {
            controller.execute(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
